﻿package test;

public class Fraction {

    private int fenzi;
    private int fenmu;

    public Fraction() {
        super();
    }

    public Fraction(int fenzi, int fenmu) {
        super();
        this.fenzi = fenzi;
        this.fenmu = fenmu;
        this.simplify(fenzi, fenmu); //在构造函数中直接化简分数（感觉此方法欠妥）
    }

    public static Fraction add(Fraction f1, Fraction f2) {

        int fz1 = f1.getFenzi();
        int fz2 = f2.getFenzi();
        int fm1 = f1.getFenmu();
        int fm2 = f2.getFenmu();
        Fraction f = new Fraction(fz1 * fm2 + fm1 * fz2, fm1 * fm2);
        return f;

    }//加法运算

    public static Fraction sub(Fraction f1, Fraction f2) {
        int fz1 = f1.getFenzi();
        int fz2 = f2.getFenzi();
        int fm1 = f1.getFenmu();
        int fm2 = f2.getFenmu();
        Fraction f = new Fraction(fz1 * fm2 - fm1 * fz2, fm1 * fm2);
        return f;
    }//减法运算

    public static Fraction mul(Fraction f1, Fraction f2) {
        int fz1 = f1.getFenzi();
        int fz2 = f2.getFenzi();
        int fm1 = f1.getFenmu();
        int fm2 = f2.getFenmu();
        Fraction f = new Fraction(fz1 * fz2, fm1 * fm2);
        return f;
    }//乘法运算

    public static Fraction div(Fraction f1, Fraction f2) {
        int fz1 = f1.getFenzi();
        int fz2 = f2.getFenzi();
        int fm1 = f1.getFenmu();
        int fm2 = f2.getFenmu();
        Fraction f = new Fraction(fz1 * fm2, fm1 * fz2);
        return f;
    }//除法运算

    public static boolean compare(Fraction f1, Fraction f2) {
        int fz1 = f1.getFenzi();
        int fz2 = f2.getFenzi();
        int fm1 = f1.getFenmu();
        int fm2 = f2.getFenmu();
        if (fz1 * fm2 >= fz2 * fm1) {
            return true;
        } else {
            return false;
        }
    }//比较两分数的大小

    public static int GCD(int m, int n) {
        while (true) {
            if ((m = m % n) == 0) {
                return n;
            }
            if ((n = n % m) == 0) {
                return m;
            }
        }
    }//计算最大公约数

    public void simplify(int fenzi, int fenmu) {
        int GCD = GCD(fenzi, fenmu);
        this.fenzi = fenzi / GCD;
        this.fenmu = fenmu / GCD;
    }//化简分数

    @Override
    public String toString() {
        if (fenzi == 0) {
            return 0 + "";
        } else if (fenzi % fenmu == 0) {
            return fenzi / fenmu + "";
        } else {
            return fenzi + "/" + fenmu;
        }
    }//改写toString，输出为分数形式

    public int getFenzi() {
        return fenzi;
    }

    public void setFenzi(int fenzi) {
        this.fenzi = fenzi;
    }

    public int getFenmu() {
        return fenmu;
    }

    public void setFenmu(int fenmu) {
        this.fenmu = fenmu;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + fenmu;
        result = prime * result + fenzi;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Fraction other = (Fraction) obj;
        if (fenmu != other.fenmu) {
            return false;
        }
        if (fenzi != other.fenzi) {
            return false;
        }
        return true;
    }

    public static Fraction CreatFrac() {
        int fz, fm;
        fz = (int) (0 + Math.random() * (100 - 0 + 1));//分子取0--100的随机数
        fm = (int) (1 + Math.random() * (10 - 1 + 1));//分母取1--100的随机数
        Fraction frac = new Fraction(fz, fm);
        return frac;
    }//创建随机分数

}
